#!/bin/bash
set -e

# 获取版本号
VERSION=$(grep '"version"' data/Frames.data | awk -F'"' '{print $4}')

# 创建输出目录
OUTPUT_DIR="dist/packaging"
mkdir -p "$OUTPUT_DIR"

# 生成 deb 包
if command -v dpkg-buildpackage &> /dev/null; then
    echo "Building Debian package..."
    cd debian
    debuild -us -uc
    cp "../gdradio_${VERSION}_amd64.deb" "$OUTPUT_DIR/"
    cd ..
fi

# 生成 Flatpak
if command -v flatpak-builder &> /dev/null; then
    echo "Building Flatpak..."
    flatpak-builder --repo=myrepo --force build-dir org.gdradio.app.json
    flatpak build-bundle myrepo "$OUTPUT_DIR/gdradio-${VERSION}.flatpak" org.gdradio.app
fi

# 生成 Snap
if command -v snapcraft &> /dev/null; then
    echo "Building Snap..."
    snapcraft
    mv gdradio_*.snap "$OUTPUT_DIR/"
fi

# 生成玲珑包（仅限Deepin）
if command -v deepin-builder &> /dev/null; then
    echo "Building Linglong package..."
    deepin-builder build gdradio.json
    cp gdradio.lapp "$OUTPUT_DIR/gdradio-${VERSION}.lapp"
fi

echo "Packages created in $OUTPUT_DIR"